﻿using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

namespace Nirvana
{

    [CustomEditor(typeof(FontMaker), true)]
    public class FontMakerEditor : Editor
    {
        private class FontMakerTool
        {
            public SerializedProperty property;
            public FontMakerEditor fonteditor;

            public void Draw(Rect A_1, int A_2, bool A_3, bool A_4)
            {
                this.fonteditor.DrawFontItem(this.property, A_1, A_2, A_3, A_4);
            }
        }

        private SerializedProperty atlasName;
        private SerializedProperty padding;
        private SerializedProperty maxAtlasSize;
        private SerializedProperty fonts;
        private ReorderableList fontAtlaslist;
        private Dictionary<string, ReorderableList> fontAtlasMap = new Dictionary<string, ReorderableList>(StringComparer.Ordinal);
        private static ReorderableList.HeaderCallbackDelegate DrawHeaderCallback;

        public override void OnInspectorGUI()
        {
            base.serializedObject.Update();
            EditorGUILayout.PropertyField(this.atlasName, new GUILayoutOption[0]);
            EditorGUILayout.PropertyField(this.padding, new GUILayoutOption[0]);
            EditorGUILayout.PropertyField(this.maxAtlasSize, new GUILayoutOption[0]);

            this.fontAtlaslist.DoLayoutList();
            base.serializedObject.ApplyModifiedProperties();
            FontMaker fontMaker = (FontMaker)this.target;
            if (GUILayout.Button("Build", new GUILayoutOption[0]))
            {
                fontMaker.Build();
            }

        }

        private void OnEnable()
        {
            if (this.target == null)
            {
                return;
            }
            SerializedObject serializedObject = base.serializedObject;
            this.atlasName = serializedObject.FindProperty("atlasName");
            this.padding = serializedObject.FindProperty("padding");
            this.maxAtlasSize = serializedObject.FindProperty("maximumAtlasSize");
            this.fonts = serializedObject.FindProperty("fonts");
            this.fontAtlaslist = new ReorderableList(serializedObject, this.fonts);
            ReorderableList _reorderlist = this.fontAtlaslist;
            if (DrawHeaderCallback == null)
            {
                DrawHeaderCallback = new ReorderableList.HeaderCallbackDelegate(DrawHeader);
            }
            _reorderlist.drawHeaderCallback = DrawHeaderCallback;
            this.fontAtlaslist.elementHeightCallback = new ReorderableList.ElementHeightCallbackDelegate(this.DrawElementHeight);
            this.fontAtlaslist.drawElementCallback = new ReorderableList.ElementCallbackDelegate(this.DrawElement);
        }

        private static void DrawHeader(Rect rect)
        {
            GUI.Label(rect, "Fonts:");
        }
        private float DrawElementHeight(int height)
        {
            return this.DrawElementHeight(this.fonts, height);
        }
        private float DrawElementHeight(SerializedProperty fonts, int height)
        {
            SerializedProperty arrayElementAtIndex = fonts.GetArrayElementAtIndex(height);
            if (!arrayElementAtIndex.isExpanded)
            {
                return EditorGUIUtility.singleLineHeight;
            }
            SerializedProperty serializedProperty = arrayElementAtIndex.FindPropertyRelative("glyphs");
            if (serializedProperty.arraySize > 0)
            {
                return (4.75f + (float)serializedProperty.arraySize) * EditorGUIUtility.singleLineHeight;
            }
            return 5.75f * EditorGUIUtility.singleLineHeight;
        }

        private void DrawElement(Rect A_1, int A_2, bool A_3, bool A_4)
        {
            this.DrawElement(this.fonts, A_1, A_2, A_3, A_4);
        }
        private void DrawElement(SerializedProperty fonts, Rect A_2, int A_3, bool A_4, bool A_5)
        {
            SerializedProperty arrayElementAtIndex = fonts.GetArrayElementAtIndex(A_3);
            A_2.x += 10f;
            A_2.width -= 10f;
            Rect rect = new Rect(A_2.x, A_2.y, A_2.width, EditorGUIUtility.singleLineHeight);
            arrayElementAtIndex.isExpanded = EditorGUI.Foldout(rect, arrayElementAtIndex.isExpanded, new GUIContent(arrayElementAtIndex.displayName));
            if (!arrayElementAtIndex.isExpanded)
            {
                return;
            }
            SerializedProperty serializedProperty = arrayElementAtIndex.FindPropertyRelative("fontName");
            SerializedProperty serializedProperty2 = arrayElementAtIndex.FindPropertyRelative("glyphs");
            rect.y += 1.25f * EditorGUIUtility.singleLineHeight;
            EditorGUI.PropertyField(rect, serializedProperty, new GUIContent("Font Name:"));
            rect.y += EditorGUIUtility.singleLineHeight;
            rect.height = A_2.y - EditorGUIUtility.singleLineHeight;
            ReorderableList reorderableList = this.GetReorderableList(serializedProperty2);
            reorderableList.DoList(rect);
        }

        private ReorderableList GetReorderableList(SerializedProperty A_1)
        {
            FontMakerTool fonttool = new FontMakerTool();
            fonttool.property = A_1;
            fonttool.fonteditor = this;
            ReorderableList reorderableList;
            if (this.fontAtlasMap.TryGetValue(fonttool.property.propertyPath, out reorderableList))
            {
                return reorderableList;
            }
            reorderableList = new ReorderableList(fonttool.property.serializedObject, fonttool.property);
            reorderableList.drawHeaderCallback = new ReorderableList.HeaderCallbackDelegate(this.DrawFontItem);
            reorderableList.elementHeight = EditorGUIUtility.singleLineHeight;
            reorderableList.drawElementCallback = new ReorderableList.ElementCallbackDelegate(fonttool.Draw);
            this.fontAtlasMap.Add(fonttool.property.propertyPath, reorderableList);
            return reorderableList;
        }

        private void DrawFontItem(Rect A_1)
        {
            Rect position = new Rect(A_1.x + 10f, A_1.y, A_1.width / 4f, A_1.height);
            Rect position2 = new Rect(A_1.x + 10f + A_1.width / 4f, A_1.y, A_1.width / 4f, A_1.height);
            Rect position3 = new Rect(A_1.x + 10f + A_1.width / 2f, A_1.y, A_1.width / 2f, A_1.height);
            GUI.Label(position, "Character:");
            GUI.Label(position2, "Code:");
            GUI.Label(position3, "Image:");
        }

        private void DrawFontItem(SerializedProperty A_1, Rect A_2, int A_3, bool A_4, bool A_5)
        {
            SerializedProperty arrayElementAtIndex = A_1.GetArrayElementAtIndex(A_3);
            SerializedProperty serializedProperty = arrayElementAtIndex.FindPropertyRelative("code");
            SerializedProperty serializedProperty2 = arrayElementAtIndex.FindPropertyRelative("image");
            Rect rect = new Rect(A_2.x, A_2.y, A_2.width / 4f, A_2.height);
            Rect rect2 = new Rect(A_2.x + A_2.width / 4f, A_2.y, A_2.width / 4f, A_2.height);
            Rect rect3 = new Rect(A_2.x + A_2.width / 2f, A_2.y, A_2.width / 2f, A_2.height);
            try
            {
                string text = char.ConvertFromUtf32(serializedProperty.intValue);
                text = EditorGUI.TextField(rect, text);
                serializedProperty.intValue = char.ConvertToUtf32(text, 0);
            }
            catch (Exception)
            {
            }
            GUI.enabled = false;
            EditorGUI.IntField(rect2, serializedProperty.intValue);
            GUI.enabled = true;
            EditorGUI.BeginChangeCheck();
            EditorGUI.PropertyField(rect3, serializedProperty2, GUIContent.none);
            int intValue;
            if (EditorGUI.EndChangeCheck() && serializedProperty.intValue == 0 && int.TryParse(serializedProperty2.objectReferenceValue.name, out intValue))
            {
                serializedProperty.intValue = intValue;
            }
        }
    }
}

